@echo off
cls
echo HarmonyOS Application Deployment Script (Batch Version)
echo --------------------------------------------------

:: Configuration
set BUNDLE_NAME=cn.edu.neusoft.myapplication
set ABILITY_NAME=EntryAbility
set MODULE_NAME=entry

:: Check if hdc is available
where hdc >nul 2>nul
if %errorlevel% neq 0 (
    echo Error: hdc command not found. Please ensure HarmonyOS SDK is properly configured.
    pause
    exit /b 1
)

:: Check device connection
echo.
echo Checking device connection...
hdc list targets
if %errorlevel% neq 0 (
    echo Error: No device connected. Please ensure HarmonyOS 6.0 emulator is running.
    pause
    exit /b 1
)

:: Install dependencies
echo.
echo Installing dependencies...
ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl true
if %errorlevel% neq 0 (
    echo Error: Failed to install dependencies.
    pause
    exit /b 1
)

:: Build project
echo.
echo Building project...
hvigorw assembleApp
if %errorlevel% neq 0 (
    echo Error: Build failed.
    pause
    exit /b 1
)

:: Find and install HAP package
echo.
echo Installing application...

:: Try to stop any existing instance
hdc shell aa force-stop %BUNDLE_NAME% 2>nul

:: Try different HAP package paths
if exist "entry\build\default\outputs\default\entry-default-unsigned.hap" (
    hdc install "entry\build\default\outputs\default\entry-default-unsigned.hap"
) else if exist "entry\build\default\outputs\default\entry-default.hap" (
    hdc install "entry\build\default\outputs\default\entry-default.hap"
) else (
    echo Error: HAP package not found. Please check build output.
    pause
    exit /b 1
)

if %errorlevel% neq 0 (
    echo Error: Failed to install application.
    pause
    exit /b 1
)

:: Start application
echo.
echo Starting application...
hdc shell aa start -a %ABILITY_NAME% -b %BUNDLE_NAME% -m %MODULE_NAME%
if %errorlevel% neq 0 (
    echo Warning: Application installed but failed to start automatically.
) else (
    echo Success: Application deployed and started on HarmonyOS 6.0 emulator!
)

echo --------------------------------------------------
pause